<template>
    <div class="carManager">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 查询自己的流程该谁处理了
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <!--<div class="handle-box">-->
            <!--<el-button-->
            <!--type="primary"-->
            <!--icon="el-icon-search"-->
            <!--@click="addVisible = true"-->
            <!--class="tianjia"-->
            <!--&gt;添加-->
            <!--</el-button>-->
            <!--</div>-->
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
            >
                <el-table-column type="index" width="55" align="center" label="序号" class-name="table"></el-table-column>
                <!-- <el-table-column
                  prop="joinTime"
                  :formatter="dateFormatter"
                  label="开始日期"
                  sortable
                  width="150"
                  class-name="table"
                ></el-table-column>
                <el-table-column
                  prop="expirationTime"
                  :formatter="dateFormatterexpirationTime"
                  label="结束日期"
                  sortable
                  width="150"
                  class-name="table"
                ></el-table-column>-->
                <el-table-column prop="askForLeaveId" label="请假id" width="120" class-name="table"></el-table-column>
                <el-table-column prop="cause" label="原因" width="100" class-name="table"></el-table-column>
                <el-table-column prop="agentName" label="审批人" width="100" class-name="table"></el-table-column>
                <!--<el-table-column prop="traceReason" label="理由" width="120" class-name="table"></el-table-column>-->
                <!--<el-table-column prop="traceResult" label="结果" width="120" class-name="table"></el-table-column>-->
                <!--<el-table-column prop="traceCreatetime" label="创建时间" class-name="table"></el-table-column>-->
                <!--<el-table-column prop="traceNewadd" label="新加" class-name="table"></el-table-column>-->
                <!--<el-table-column prop="traceOldadd" label="原有" class-name="table"></el-table-column>-->
                <!--<el-table-column prop="traceState" label="声明" class-name="table"></el-table-column>-->
                <!--<el-table-column prop="staffName" label="员工" width="180" class-name="table"></el-table-column>-->
                <!--<el-table-column prop="agentName" label="代理商" width="100" class-name="table"></el-table-column>-->
                <!--<el-table-column prop="deviceName" label="设备" width="100" class-name="table"></el-table-column>-->
                <!--<el-table-column label="操作" width="180" align="center" class-name="table">-->
                    <!--<template slot-scope="scope">-->
                        <!--<el-button-->
                                <!--type="text"-->
                                <!--icon="el-icon-edit"-->
                                <!--@click="handleEdit(scope.$index, scope.row)"-->
                        <!--&gt;编辑-->
                        <!--</el-button>-->
                        <!--<el-button-->
                                <!--type="text"-->
                                <!--icon="el-icon-delete"-->
                                <!--class="red"-->
                                <!--@click="handleDelete(scope.$index, scope.row)"-->
                        <!--&gt;删除-->
                        <!--</el-button>-->
                    <!--</template>-->
                <!--</el-table-column>-->
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="cur_page"
                        :page-size="pagesize"
                        :total="totalRecords"
                        :page-sizes="[5, 10, 20, 40]"
                        layout="total, sizes, prev, pager, next, jumper"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="editForm" :model="editForm" label-width="100px" :rules="FormRules">
                <el-form-item label="编码" prop="traceCode">
                    <el-input v-model="editForm.traceCode"></el-input>
                </el-form-item>
                <el-form-item label="容量" prop="traceContent">
                    <el-input v-model="editForm.traceContent"></el-input>
                </el-form-item>
                <el-form-item label="理由" prop="traceReason">
                    <el-input v-model="editForm.traceReason"></el-input>
                </el-form-item>
                <el-form-item label="结果" prop="traceResult">
                    <el-input v-model="editForm.traceResult"></el-input>
                </el-form-item>
                <el-form-item label="新加" prop="traceNewadd">
                    <el-input v-model="editForm.traceNewadd"></el-input>
                </el-form-item>
                <el-form-item label="原有" prop="traceOldadd">
                    <el-input v-model="editForm.traceOldadd"></el-input>
                </el-form-item>
                <el-form-item label="员工" prop="traceStaffid">
                    <el-select v-model="editForm.traceStaffid" placeholder="员工">
                        <el-option
                                v-for="item in staff"
                                :key="item.traceStaffid"
                                :label="item.value"
                                :value="item.traceStaffid"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="代理商" prop="traceAgentid">
                    <el-select v-model="editForm.traceAgentid" placeholder="代理商">
                        <el-option
                                v-for="item in agent"
                                :key="item.traceAgentid"
                                :label="item.value"
                                :value="item.traceAgentid"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="设备" prop="traceDeviceid">
                    <el-select v-model="editForm.traceDeviceid" placeholder="设备">
                        <el-option
                                v-for="item in dev"
                                :key="item.traceDeviceid"
                                :label="item.value"
                                :value="item.traceDeviceid"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
        </el-dialog>
        <!-- 添加弹出框 -->
        <el-dialog title="添加" :visible.sync="addVisible" width="30%">
            <el-form ref="addForm" :model="addForm" :rules="FormRules" label-width="100px">
                <el-form-item label="编码" prop="traceCode">
                    <el-input v-model="addForm.traceCode"></el-input>
                </el-form-item>
                <el-form-item label="容量" prop="traceContent">
                    <el-input v-model="addForm.traceContent"></el-input>
                </el-form-item>
                <el-form-item label="理由" prop="traceReason">
                    <el-input v-model="addForm.traceReason"></el-input>
                </el-form-item>
                <el-form-item label="结果" prop="traceResult">
                    <el-input v-model="addForm.traceResult"></el-input>
                </el-form-item>
                <el-form-item label="新加" prop="traceNewadd">
                    <el-input v-model="addForm.traceNewadd"></el-input>
                </el-form-item>
                <el-form-item label="原有" prop="traceOldadd">
                    <el-input v-model="addForm.traceOldadd"></el-input>
                </el-form-item>
                <el-form-item label="员工" prop="traceStaffid">
                    <el-select v-model="addForm.traceStaffid" placeholder="员工">
                        <el-option
                                v-for="item in staff"
                                :key="item.traceStaffid"
                                :label="item.value"
                                :value="item.traceStaffid"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="代理商" prop="traceAgentid">
                    <el-select v-model="addForm.traceAgentid" placeholder="代理商">
                        <el-option
                                v-for="item in agent"
                                :key="item.traceAgentid"
                                :label="item.value"
                                :value="item.traceAgentid"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="设备" prop="traceDeviceid">
                    <el-select v-model="addForm.traceDeviceid" placeholder="设备">
                        <el-option
                                v-for="item in dev"
                                :key="item.traceDeviceid"
                                :label="item.value"
                                :value="item.traceDeviceid"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
        </el-dialog>
        <!-- 添加弹出框 结束-->

        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRow">确 定</el-button>
      </span>
        </el-dialog>
    </div>
</template>

<script>

    import Validator from "async-validator";

    export default {
        name: "carManager",
        data() {
            return {
                dev: [],
                agent: [],
                staff: [],
                value: "",
                tableData: [],
                cur_page: 1,
                pagesize: 20,
                totalRecords: 0,
                totalPages: 0,
                multipleSelection: [],
                select_cate: "",
                select_word: "",
                del_list: [],
                is_search: false,
                editVisible: false,
                addVisible: false,
                delVisible: false,
                editForm: {
                    traceId: "",
                    traceCode: "",
                    traceReason: "",
                    traceContent: "",
                    traceResult: "",
                    traceNewadd: "",
                    traceOldadd: "",
                    traceStaffid: "",
                    traceAgentid: "",
                    traceDeviceid: ""
                },
                FormRules: {
                    traceAgentid: [{message: "代理商不能为空", required: true, trigger: "blur"}],
                    traceStaffid: [{message: "员工不能为空", required: true, trigger: "blur"}],
                    traceDeviceid: [{message: "设备不能为空", required: true, trigger: "blur"}]
                },
                addForm: {
                    traceId: "",
                    traceCode: "",
                    traceReason: "",
                    traceContent: "",
                    traceResult: "",
                    traceNewadd: "",
                    traceOldadd: "",
                    traceStaffid: "",
                    traceAgentid: "",
                    traceDeviceid: ""
                },
                carFormRules: {},
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getData()
            // this.getstaff()
            // this.getdev()
            // this.getagent()
        },
        computed: {
            data() {
                return this.tableData.filter(d => {
                    let is_del = false;
                    for (let i = 0; i < this.del_list.length; i++) {
                        if (d.name === this.del_list[i].name) {
                            is_del = true;
                            break;
                        }
                    }
                    if (!is_del) {
                        if (
                            d.address.indexOf(this.select_cate) > -1 &&
                            (d.name.indexOf(this.select_word) > -1 ||
                                d.address.indexOf(this.select_word) > -1)
                        ) {
                            return d;
                        }
                    }
                });
            }
        },
        methods: {
            //获取设备
            getdev() {
                var res = this;
                this.$axios({
                    url: "/api/waterDevice/findAllDevice",
                    method: "get",
                    data: {pageNum: res.page, pageSize: res.pagesize}
                })
                    .then(function (response) {
                        if (response.status <= 200) {
                            for (var i = 0; i < response.data.length; i++) {
                                res.dev.push({
                                    value: response.data[i].deviceName,
                                    traceDeviceid: response.data[i].deviceId
                                });
                            }
                        }
                    })
                    .catch(function (error) {
                        res.$message.error("查询失败: " + error);
                        console.log(error);
                    });
            },
            //页面初始化时获取下拉框里面的数据 【代理商】
            getagent() {
                var res = this;
                this.$axios({
                    url: '/api/waterAgent/selectWaterAgentAll',
                    method: 'post',
                    data: {pageNum: res.cur_page, pageSize: res.pagesize}
                })
                    .then(function (response) {
                        if (response.status <= 200) {
                            for (var i = 0; i < response.data.data.list.length; i++) {
                                res.agent.push({
                                    value: response.data.data.list[i].agentName,
                                    traceAgentid: response.data.data.list[i].agentId
                                });
                            }
                        }
                    })
                    .catch(function (error) {
                        res.$message.error('查询失败: ' + error);
                        console.log(error);
                    });
            },
            //获取员工
            getstaff() {
                var res = this;
                this.$axios({
                    url: "/api/waterStaff/findAllStaff",
                    method: "get",
                    data: {pageNum: res.page, pageSize: res.pagesize}
                })
                    .then(function (response) {
                        if (response.status <= 200) {
                            for (var i = 0; i < response.data.length; i++) {
                                res.staff.push({
                                    value: response.data[i].staffName,
                                    traceStaffid: response.data[i].staffId
                                })
                            }
                        }
                    })
                    .catch(function (error) {
                        res.$message.error("查询失败: " + error);
                        console.log(error);
                    });
            },
            dateFormatterexpirationTime(row, column) {
                let datetime = row.expirationTime;
                if (datetime) {
                    datetime = new Date(datetime);
                    let y = datetime.getFullYear() + "-";
                    let mon = datetime.getMonth() + 1 + "-";
                    let d = datetime.getDate();
                    return y + mon + d;
                }
                return "";
            },
            dateFormatter(row, column) {
                let datetime = row.joinTime;
                if (datetime) {
                    datetime = new Date(datetime);
                    let y = datetime.getFullYear() + "-";
                    let mon = datetime.getMonth() + 1 + "-";
                    let d = datetime.getDate();
                    return y + mon + d;
                }
                return "";
            },
            //每页显示数改变
            handleSizeChange(val) {
                this.pagesize = val;
                this.getData();
            },
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },
            // 获取 easy-mock 的模拟数据

            getData() {
                var res = this;

                this.$axios({
                    url: "/api/askForLeave/selectFlowDisposeByLoginId/" + localStorage.getItem("loginId"),
                    method: "get",

                })
                    .then(function (response) {
                        console.log(response)
                        if (response.data.code ===  200){
                            res.tableData = response.data.data;
                            // res.totalRecords = response.data.length; //总条数
                        }

                    })
                    .catch(function (error) {
                        // res.$message.error("查询失败: " + error);
                        // console.log(error);
                    });
            },
            formatter(row, column) {
                return row.address;
            },
            filterTag(value, row) {
                return row.tag === value;
            },

            handleEdit(index, row) {
                this.idx = index;
                this.id = row.traceId;
                this.editForm = {
                    traceId: row.traceId,
                    traceCode: row.traceCode,
                    aprName: row.aprName,
                    traceReason: row.traceReason,
                    traceContent: row.traceContent,
                    traceResult: row.traceResult,
                    traceNewadd: row.traceNewadd,
                    traceOldadd: row.traceOldadd,
                    traceCreatetime: row.traceCreatetime,
                    traceState: row.traceState,
                    staffName: row.staffName,
                    deviceName: row.deviceName,
                    traceStaffid: row.traceStaffid,
                    traceAgentid: row.traceAgentid,
                    traceDeviceid: row.traceDeviceid
                };
                this.editVisible = true;
            },
            handleDelete(index, row) {
                this.idx = index;
                this.id = row.traceId;
                this.delVisible = true;
            },
            // 保存编辑
            saveEdit() {
                var res = this;
                var setData = this.editForm;
                this.$refs.editForm.validate(valid => {
                    if (!valid) {
                        res.$message.success("格式不正确");
                    } else {
                        this.$axios({
                            url: "/api/waterTrace",
                            method: "put",
                            data: setData
                        })
                            .then(function (response) {
                                if (response.status <= 200) {
                                    res.editVisible = false;
                                    res.$message.success("修改成功");
                                    res.$message.success(`修改第 ${res.idx + 1} 行成功`);
                                    res.getData()

                                    if (res.tableData[res.idx].id === res.id) {
                                        res.$set(res.tableData, res.idx, res.editForm);
                                    } else {
                                        for (let i = 0; i < res.tableData.length; i++) {
                                            if (res.tableData[i].id === res.id) {
                                                res.$set(res.tableData, i, res.editForm);
                                                return;
                                            }
                                        }
                                    }
                                }
                            })
                            .catch(function (error) {
                                res.$message.success("修改失败！");
                                console.log(error);
                            });
                    }

                    // 验证通过后的处理...
                });
            },
            // 添加
            saveAdd() {
                var res = this;
                var setData = this.addForm;
                console.log(setData)
                this.$refs.addForm.validate(valid => {
                    if (!valid) {
                        res.$message.success("格式不正确");
                    } else {
                        this.$axios({
                            url: "/api/waterTrace",
                            method: "post",
                            data: setData
                        })
                            .then(function (response) {
                                if (response.status <= 200) {
                                    if (response.data.code == 0) {
                                        res.$message.error("已存在");
                                    } else {
                                        res.addVisible = false;
                                        console.log(response.data);
                                        res.$message.success("添加成功");
                                        res.getData()
                                    }
                                }
                            })
                            .catch(function (error) {
                                res.$message.error("添加失败！");
                                console.log(error);
                            });
                    }
                });
            },
            // 确定删除
            deleteRow() {
                var res = this;
                console.log(this.id);
                this.$axios({
                    url: "/api/waterTrace?id=" + res.id,
                    method: "delete",
                    data: {id: res.id}
                })
                    .then(function (response) {
                        if (response.status <= 200) {
                            res.$message.success("删除成功");
                            res.delVisible = false;
                            res.getData();
                            if (res.tableData[res.idx].id === res.id) {
                                res.tableData.splice(res.idx, 1);
                            } else {
                                for (let i = 0; i < res.tableData.length; i++) {
                                    if (res.tableData[i].id === res.id) {
                                        res.tableData.splice(i, 1);
                                        return;
                                    }
                                }
                            }
                        }
                    })
                    .catch(function (error) {
                        res.$message.success("查询失败！");
                        console.log(error);
                    });
            }
        }
    };
</script>

<style scoped>
    .huizong {
        height: 30px;
        width: 400px;
        position: relative;
    }

    .huizong span {
        height: 30px;
        width: 400px;
        position: absolute;
        bottom: -60px;
        z-index: 999;
    }

    .tianjia {
        margin-left: 10px;
    }

    .sousuo {
        margin-right: 10px;
    }

    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .del-dialog-cnt {
        font-size: 16px;
        text-align: center;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .outup {
        font-size: 12px;
        color: #fff;
        background-color: #409eff;
        display: inline-block;
        border-radius: 3px;
        padding: 8px 15px;
        margin-left: 10px;
        margin-top: 3px;
    }

    .upload-demo {
        height: 30px;
        width: 100px;
        display: inline-block;
        margin-left: 10px;
    }

    .inup {
        margin-top: 10px;
    }
</style>
<style>
    .el-upload--text {
        width: 60px;
        height: 30px;
        display: inline-block;
        border: 0;
        overflow: visible;
    }

    .el-table--border {
        border: 1px solid black;
    }

    .el-table--border,
    .el-table--border th,
    .el-table td,
    .el-table th {
        border-right: 1px solid black;
        border-bottom-color: black;
        border-left-color: black;
    }

    .table {
        border-bottom-color: black !important;
        border-left-color: black !important;
    }
</style>

